<?php
namespace jy_weishop;
defined('BY_JYA') or exit('error');
/*
	任务
*/
class task extends \plugin{
	/*
		订单状态UP
	*/
	public function orderup($ismanage=false){
		$shopset=$this->plugin_setting('shopset');
		$params=array('uniacid'=>$_SESSION['uniacid'],'deleted'=>0);
		$pindex = max(1, 0);
		$psize = 10;
		$total='';
		if(!$ismanage){
			$params['mid']=$_SESSION['uid'];
		}
		//关闭订单
		$params['status']=0;
		$shopset['orderpay_outtime']=intval($shopset['orderpay_outtime'])?:24;
		$orderpay_outtime=TIMESTAMP-intval($shopset['orderpay_outtime'])*3600;
		$params['createtime <']=$orderpay_outtime;
		$order = pdo_getslice('jy_weishop_order',$params,array($pindex,$psize),$total,array('id'));
		if($order){
			foreach($order as $a){
				if($a['id']){
					$this->model('order')->order_cancel($a['id'],true);
				}			
			}
		}
		//确认收货
		$params['status']=2;
		$shopset['ordercheck_outtime']=intval($shopset['ordercheck_outtime'])?:7;
		$ordercheck_outtime=TIMESTAMP-intval($shopset['ordercheck_outtime'])*24*3600;
		$params['sendtime <']=$ordercheck_outtime;
		$order = pdo_getslice('jy_weishop_order',$params,array($pindex,$psize),$total,array('id'));
		if($order){
			foreach($order as $a){
				$getgoods=pdo_getall('jy_weishop_order_goods',array('uniacid'=>$_SESSION['uniacid'],'orderid'=>$a['id']),array('goodsid','total','optionid'));
				//核销商品,不确认完成
				$isverify=false;
				foreach($getgoods as $item){
					$g=$this->get('goods',array('id'=>$item['goodsid']),array('type','isverify'));
					if($g['isverify']==1){
						$isverify=true;
					}
				}
				if(!$isverify){
					$this->model('order')->order_receive($a['id']);
				}
			}
		}
		//已支付，未发货订单，超时完成
		if(intval($shopset['orderfinish_outtime'])>0){
			$params['status']=1;
			$orderfinish_outtime=TIMESTAMP-intval($shopset['orderfinish_outtime'])*3600;
			$params['createtime <']=$orderfinish_outtime;
			$order = pdo_getslice('jy_weishop_order',$params,array($pindex,$psize),$total,array('id'));
			if($order){
				foreach($order as $a){
					$this->model('order')->order_receive($a['id']);
				}
			}
		}
		//删除活动订单
		$this->model('activity')->order_task();
	}
	/*
		待领取优惠券弹窗任务
	*/
	public function pop_coupon_wait(){
		if($_SESSION['uid']){
			$giveall=$this->pdo_getall('coupon_wait',array('mid'=>$_SESSION['uid'],'status'=>0));
			if($giveall){
				foreach($giveall as $k=>&$give){
					$ac_types=array('huodong'=>'活动送券','goods'=>'购物送券','share'=>'分享送券','new'=>'新人发券','enough'=>'满额送券','coupon'=>'用券送券');
					$give['typetitle']=$ac_types[$give['ac_type']];
					if($give['ac_type']=='huodong'){
						$sendtasktable='jy_weishop_coupon_huodongsendtask';
					}elseif($give['ac_type']=='goods'){
						$sendtasktable='jy_weishop_coupon_goodsendtask';
					}elseif($give['ac_type']=='share'){
						$sendtasktable='jy_weishop_coupon_sendticket_share';
					}elseif($give['ac_type']=='new'){
						$sendtasktable='jy_weishop_coupon_sendticket';
					}elseif($give['ac_type']=='enough' || $give['ac_type']=='order'){
						$sendtasktable='jy_weishop_coupon_sendtasks';
					}elseif($give['ac_type']=='coupon'){
						$sendtasktable='jy_weishop_coupon_usesendtasks';
					}
					if(!$sendtasktable){
						continue;
					}
					$couponids=pdo_getcolumn($sendtasktable,array('id'=>$give['ac_id']),'couponid');
					if(!is_numeric($couponids)){
						$couponids=str_array($couponids);
						foreach($couponids as $cv){
							$couponid=pdo_getcolumn('jy_weishop_coupon',array('uniacid'=>$_SESSION['uniacid'],'id'=>$cv),'id');
							if($couponid){
								break;
							}
						}
						$total=count($couponids);
					}else{
						$couponid=$couponids;
						$total=1;
					}
					$coupon=pdo_get('jy_weishop_coupon',array('uniacid'=>$_SESSION['uniacid'],'id'=>$couponid),array('id','backtype','discount','deduct','sendtitle','sendimage','senddesc'));
					if(!$coupon['id']){
						unset($giveall[$k]);
						continue;
					}
					$nickname=pdo_getcolumn('core_members',array('id'=>$_SESSION['uid']),'nickname');
					$give['sendtitle']=str_replace('[nickname]',$nickname,$coupon['sendtitle']);
					$give['sendtitle']=str_replace('[total]',$total,$give['sendtitle']);
					$give['sendimage']=$coupon['sendimage'];
					$give['senddesc']=$coupon['senddesc'];
					if($coupon['backtype']==0){
							$give['fee']=intval($coupon['deduct']);
					}elseif($coupon['backtype']==1){
							$give['fee']=$coupon['discount'].'折';
					}elseif($coupon['backtype']==2){
							$give['fee']='立返';
					}
					
				}
				return $giveall;
			}
		}
		return false;
	}
	/*
		优惠券弹窗任务
	*/
	public function pop_coupon_hd(){
		if($_SESSION['uid']){
			$params=array('uniacid'=>$_SESSION['uniacid'],'status'=>1);
			$huodongs=pdo_getall('jy_weishop_coupon_huodongsendtask',$params);
			foreach($huodongs as $k=>&$item){
				$item['limittime']=json_decode($item['limittime'],true);
				$isgive=$this->pdo_get('coupon_wait',array('ac_type'=>'huodong','ac_id'=>$item['id'],'uniacid'=>$_SESSION['uniacid'],'status'=>1),'id');
				if(empty($isgive['id']) && $item['limittime']['tstart']<time() && $item['limittime']['tend']>time()){
					$this->model('coupon')->getcoupon($item['couponid']);
					$coupon=pdo_get('jy_weishop_coupon',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['couponid']),array('backtype','discount','deduct','sendtitle','sendimage','senddesc'));
					$item['sendtitle']=$coupon['sendtitle'];
					$item['sendimage']=$coupon['sendimage'];
					$item['senddesc']=$coupon['senddesc'];
					if($coupon['backtype']==0){
						$item['fee']=$coupon['deduct'];
					}elseif($coupon['backtype']==1){
						$item['fee']=$coupon['discount'].'折';
					}elseif($coupon['backtype']==2){
						$item['fee']='立返';
					}
					$give=array(
						'uniacid'=>$_SESSION['uniacid'],
						'mid'=>$_SESSION['uid'],
						'status'=>1,
						'createtime'=>TIMESTAMP,
						'remark'=>'活动送券',
						'ac_id'=>$item['id'],
						'ac_type'=>'huodong',
					);
					$this->pdo_insert('coupon_wait',$give);
					return $huodongs[$k];
				}
			}
		}
		return false;
	}
	/*
		优化弹窗任务
	*/
	public function pop_game(){
		if($_SESSION['uid']){
			$game=$this->pdo_get('game_wait',array('mid'=>$_SESSION['uid'],'status'=>0));
			if($game){
				$gameinfo=$this->pdo_get('game',array('id'=>$game['game_id'],'status'=>1));
				if($gameinfo){
					unset($gameinfo['id']);
					$game=array_merge($game,$gameinfo);
					$game['game_id']=$gameinfo['game_id'];
					$nickname=pdo_getcolumn('core_members',array('id'=>$_SESSION['uid']),'nickname');
					$game['sendtitle']=str_replace('[nickname]',$nickname,$game['sendtitle']);
					$game['senddesc']=str_replace('[nickname]',$nickname,$game['senddesc']);
					return $game;
				}else{
					return false;
				}
				
			}
		}
		return false;
	}
	/*
		自动评价
	*/
	public function int_comments(){
		$shopset=$this->plugin_setting('shopset');
		if($shopset['comment_task']){
			$params=array('uniacid'=>$_SESSION['uniacid'],'status'=>3,'iscomment'=>0);
			$shopset['comment_days']=$shopset['comment_days']?:1;
			$params['finishtime <']=TIMESTAMP-$shopset['comment_days']*24*3600;
			$list = pdo_getall('jy_weishop_order',$params,array('id','mid','finishtime','createtime'));
			pdo_delete('jy_weishop_goods_comments',array('createtime <'=>500000,'uniacid'=>$_SESSION['uniacid']));
			if($list){
				foreach($list as $order){
					$comments = pdo_count('jy_weishop_goods_comments',array('orderid'=>intval($order['id'])),'id');
					if(!$comments){
						$goods = pdo_getall('jy_weishop_order_goods',array('orderid'=>$order['id']),array('id','mid','goodsid'));
						foreach($goods as $g){
							$order['finishtime']=$order['finishtime']?:$order['createtime']+2*24*3600;
							$createtime=$order['finishtime']+24*3600;
							$insert=array(
								'uniacid'=>$_SESSION['uniacid'],
								'mid'=>$order['mid'],
								'show'=>1,
								'content'=>$shopset['comment_content']?:'该顾客感觉不错，给出5星好评!',
								'star'=>5,
								'goodsid'=>intval($g['goodsid']),
								'orderid'=>$order['id'],
								'createtime'=>$createtime,
							);
							$this->pdo_insert('goods_comments',$insert);
						}
					}
					$this->update('order',array('iscomment'=>1),array('id'=>$order['id'],'uniacid'=>$_SESSION['uniacid']));
				}
			}
			return false;
		}
	}
}